package com.example.exception;

import com.example.vo.Result;
import com.example.utils.SystemConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
@Slf4j
public class SqlExceptionHandler {

    /**
     * 数据库唯一字段重复添加异常
     *
     * @param e 异常
     * @return 添加内容重复错误信息
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result sqlUniqueException(SQLIntegrityConstraintViolationException e) {
        log.error(e.getMessage());

        return Result.error("重复添加", SystemConstants.RESPONSE_GENERAL_ERR);
    }
}
